package com.wind.taobao.api.trade.user.convert
{
	import com.wind.taobao.api.trade.user.model.UserCredit;
	import com.wind.taobao.core.util.TaobaoConstants;
	
	import mx.collections.ArrayCollection;


	/**
	 * 信用转换器
	 * @author fxy
	 *
	 */
	public class UserCreditConvert
	{
		/**
		 * 转换UserCredit集合
		 */
		public static function convertUserCredits(value:Object, type:String="json"):ArrayCollection
		{
			var userCredits:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							userCredits.addItem(convertUserCredit((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return userCredits;
		}

		/**
		 * 转换UserCredit
		 */
		public static function convertUserCredit(value:Object, type:String="json"):UserCredit
		{
			var userCredit:UserCredit=new UserCredit();
			if (value == null)
				return userCredit;
			if (type == TaobaoConstants.XML)
			{
				userCredit=convertXMLToUserCredit(XML(value), userCredit);
			}
			else
			{
				userCredit=convertJSONToUserCredit(value, userCredit);
			}
			return userCredit;
		}

		/**
		 * JSON转换为UserCredit
		 */
		private static function convertJSONToUserCredit(json:Object, userCredit:UserCredit):UserCredit
		{
			if (json.hasOwnProperty(TaobaoConstants.LEVEL))
			{
				userCredit.level=json[TaobaoConstants.LEVEL];
			}
			if (json.hasOwnProperty(TaobaoConstants.SCORE))
			{
				userCredit.score=json[TaobaoConstants.SCORE];
			}
			if (json.hasOwnProperty(TaobaoConstants.TOTAL_NUM))
			{
				userCredit.total_num=json[TaobaoConstants.TOTAL_NUM];
			}
			if (json.hasOwnProperty(TaobaoConstants.GOOD_NUM))
			{
				userCredit.good_num=json[TaobaoConstants.GOOD_NUM];
			}
			return userCredit;
		}

		/**
		 * XML转换为UserCredit
		 */
		private static function convertXMLToUserCredit(value:XML, userCredit:UserCredit):UserCredit
		{
			return userCredit;
		}

	}
}